home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_200 / 215_01 / bbscfile.c < prev    next >
Text File  |  1980-01-01  |  13KB  |  618 lines

  1. /*
  2.     bbscfile.c
  3.  
  4.     Support routines used by BBSc.c to do file i/o for the
  5.     message file.
  6.                 Mike Kelly
  7.  
  8.     06/12/83 v1.0    written
  9.     07/07/83 v1.0    updated
  10. */
  11.  
  12. #include "bdscio.h"
  13. #include "bbscdef.h"
  14.  
  15.  
  16. #define LASTDATE  " 07/07/83 "
  17.  
  18. #define PGMNAME "BBSCFILE "
  19. #define VERSION " 1.0 "
  20.  
  21.  
  22. /*    .    t.    e.    s.    t.    .    
  23.  
  24.  
  25. main(argc,argv)
  26. char    **argv;
  27. int    argc;
  28. {
  29.  
  30.     char    xtra[500];
  31.  
  32.     printf("starting %s\n",PGMNAME);
  33.     debug = 1;
  34.     strcpy(h_date,"01/01/83");
  35.     h_next = 1;
  36.  
  37.     hdrwrt();
  38.     hdrread();
  39.     strcpy(h_date,"02/01/83");
  40.     h_next = 4;
  41.     hdrwrt();
  42.     hdrread();
  43.     strcpy(xtra,".......160.......170.......180.......190.......200.......210.......220.......230.......240.......250.......260.......270.......280.......290");
  44.     strcpy(msg_text,"1.......10........20........30........40........50........60........70........80........90.......100.......110.......120.....128.........140.......150");
  45.     strcat(msg_text,xtra);
  46.     strcpy(msg_date,"01/01/83");
  47.     strcpy(msg_time,"01:01:10 PM");
  48.     strcpy(msg_to,"whom it may ");
  49.     strcpy(msg_from,"that guy");
  50.     strcpy(msg_pass,"abc");
  51.     strcpy(msg_subject,"this and that");
  52.     if (debug)
  53.     {
  54.         printf("after init, msg_text = ");
  55.         prthex(msg_text);
  56.         printf("\n");
  57.     }
  58.     h_next = 0;
  59.     msgwrt();
  60.     msgwrt();
  61.  
  62.     strfill(msg_text,0,800);
  63.     msgread(4);
  64.     printf("msg 4 = ");
  65.     prthex(msg_text);
  66.     printf("\n");
  67.  
  68.     msgread(0);
  69.     printf("msg 0 = ");
  70.     prthex(msg_text);
  71.     printf("\n");
  72.  
  73.     printf("ending %s\n",PGMNAME);
  74. }
  75.  
  76.     .    t.    e.    s.    t.    .    */
  77.  
  78.  
  79. hdrwrt()        /* write the header from memory variables */
  80. {            /* header is a 1 record file */
  81.     int    fd;
  82.     char    buf128[MSGSECT];
  83.  
  84.     if ((fd = open(HEADER,2)) == ERROR)    /* open i/o */
  85.     {
  86.         printf("can't open header.bbs will create it\n");
  87.         if ((fd = creat(HEADER)) == ERROR)
  88.         {
  89.             printf("can't create header.bbs - aborting\n");
  90.             return(ERROR);
  91.         }
  92.     }
  93.  
  94.     itoa(h_next_msg,h_next);    /* convert int to char */
  95.     strfill(buf128,26,MSGSECT);        /* init buf128 to all hex 1a */
  96.     sprintf(buf128,"%s~%s~",    /* build record */
  97.         h_next_msg,
  98.         h_date);
  99. #ifdef DEBUG
  100.     if (debug)
  101.     {
  102.         printf("after sprintf buf128 = ");
  103.         prthex(buf128);
  104.         printf("\n");
  105.     }
  106. #endif
  107.     write(fd,buf128,1);        /* write it */
  108.     close(fd);            /* no need to leave it open */
  109.     return(OK);
  110. }
  111.  
  112. hdrread()        /* read the header file into memory */
  113. {
  114.     int    fd,
  115.         cnt;
  116.     char    buf128[MSGSECT];
  117.  
  118.     if ((fd = open(HEADER,0)) == ERROR)    /* open input */
  119.     {
  120.         printf("can't open header.bbs \n");
  121.         h_next = 0;
  122.         h_next_msg[0] = '0';
  123.         h_date[0] = '0';
  124.         hdrwrt();
  125.     }
  126.     read(fd,buf128,1);
  127.     cnt = sscanf(buf128,"%s~%s~",
  128.             h_next_msg,
  129.             h_date);
  130. #ifdef DEBUG
  131.     if (debug)
  132.     {
  133.         printf("after sscanf buf128 = ");
  134.         prthex(buf128);
  135.         printf("\n");
  136.         printf("count returned from sscanf = %d\n",cnt);
  137.         printf("h_next_msg  = %s\n",h_next_msg);
  138.         printf("h_date = %s\n",h_date);
  139.     }
  140. #endif
  141.     close(fd);        /* no need to leave it open */
  142.  
  143.     if (cnt != 2)
  144.     {
  145.         return(ERROR);
  146.     }
  147.     h_next = atoi(h_next_msg);
  148.     return(OK);
  149. }
  150.  
  151. msgopen(how)
  152. int    how;        /* how to open 0=input, 1=output, 2=i/o */
  153. {
  154.     int    fd;
  155.  
  156.     if ((fd = open(MESSAGES,how)) == ERROR)    /* open i/o */
  157.     {
  158.         printf("can't open %s will create it\n",MESSAGES);
  159.         if ((fd = creat(MESSAGES)) == ERROR)
  160.         {
  161.             printf("can't create %s - aborting\n",MESSAGES);
  162.             return(ERROR);
  163.         }
  164.     }
  165.  
  166. #ifdef DEBUG
  167.     if (debug)
  168.     {
  169.         printf("in msgopen - fd = %d\n",fd);
  170.     }
  171. #endif
  172.     return(fd);
  173. }
  174.  
  175. msgclose(fd)
  176. int    fd;
  177. {
  178.  
  179. #ifdef DEBUG
  180.     if (debug)
  181.     {
  182.         printf("in msgclose - fd = %d\n",fd);
  183.     }
  184. #endif
  185.     return(close(fd));
  186. }
  187.  
  188. msgwrt(fd)        /* write the message file from memory variables */
  189. int    fd;        /* writes a message starting with the h_next msg # */
  190. {
  191.     int    rc,            /* return code */
  192.         cnt1,
  193.         cnt2,
  194.         len;
  195.     char    bufmsg0[MSG1MAX+1],
  196.         buf128[MSGSECT],
  197.         this1[10],
  198.         next1[10];
  199.  
  200.     rc = cnt1 = len = cnt2 = 0;
  201. #ifdef DEBUG
  202.     if (debug)
  203.     {
  204.         printf("in msgwrt\n");
  205.     }
  206. #endif
  207.     rc = seek(fd,h_next,0);        /* seek to next available sector */
  208. #ifdef DEBUG
  209.     if (debug)
  210.     {
  211.         printf("file open\n");
  212.         printf("seek = %d\n",rc);
  213.     }
  214. #endif
  215.     itoa(this1,h_next);        /* convert int to char */
  216.     h_next++;
  217.     itoa(next1,h_next);
  218.  
  219. #ifdef DEBUG
  220.     if (debug)
  221.     {
  222.         printf("before strfill of nulls\n");
  223.         printf("\n");
  224.     }
  225. #endif
  226.     strfill(buf128,0,MSGSECT);        /* init buf128 to all hex 00 */
  227.  
  228. /*
  229. *            build first piece of msg record
  230. */
  231.  
  232.     sprintf(buf128,"%s~%s~%s~%s~%s~%s~%s~%s~%s~",    /* build record */
  233.         this1,                    /* this rcd # */
  234.         next1,                    /*  points to next rcd # */
  235.         msg_delete,                /* delete byte */
  236.         msg_date,
  237.         msg_time,
  238.         msg_to,
  239.         msg_from,
  240.         msg_pass,
  241.         msg_subject);
  242. #ifdef DEBUG
  243.     if (debug)
  244.     {
  245.         printf("before write of 1st buf128 = ");
  246.         prthex(buf128);
  247.         printf("\n");
  248.     }
  249. #endif
  250.     cnt1 = tell(fd);
  251. #ifdef DEBUG
  252.     if (debug)
  253.     {
  254.         printf("tell() value = %d\n",cnt1);
  255.     }
  256. #endif
  257.     rc = write(fd,buf128,1);    /* write the first 128 byte record */
  258.                     /*  for a message record */
  259. #ifdef DEBUG
  260.     if (debug)
  261.     {
  262.         printf("after write of 1st rcd return code = %d\n",rc);
  263.     }
  264. #endif
  265. /*
  266. *            build the n+1 piece of msg record
  267. */
  268.  
  269.     len = (strlen(msg_text) / MSG1MAX) + 1; /* calc how many more 128 */
  270.                         /*  byte records to write */
  271. #ifdef DEBUG
  272.     if (debug)
  273.     {
  274.         printf("length of msg_text/128 + 1 = %d\n",len);
  275.     }
  276. #endif
  277.     cnt2 = 1;            /* init for substr */
  278.     while (len--)
  279.     {
  280.         itoa(this1,h_next);        /* calc/convert record #'s */
  281.         h_next++;
  282.         if (len == 0)
  283.         {
  284.             strcpy(next1,"0");    /* marks last 128 byte piece */
  285.         }                /*  of a msg */
  286.         else
  287.         {
  288.             itoa(next1,h_next);
  289.         }
  290.         strfill(bufmsg0,0,MSG1MAX);
  291.         substr(msg_text,bufmsg0,cnt2,MSG1MAX); /* move MSG1MAX bytes to buffer */
  292. #ifdef DEBUG
  293.         if (debug)
  294.         {
  295.             printf("after substr bufmsg0 = ");
  296.             prthex(bufmsg0);
  297.             printf("\n");
  298.         }
  299. #endif
  300.         cnt2 += MSG1MAX;        /* up cnt2 by MSG1MAX */
  301. #ifdef DEBUG
  302.         if (debug)
  303.         {
  304.             printf("after add cnt2 = %d\n",cnt2);
  305.         }
  306. #endif
  307.         strfill(buf128,0,MSGSECT);    /* init buf128 to all hex 00 */
  308.         sprintf(buf128,"%s~%s~%s~%s~",
  309.             this1,            /* this rcd # */
  310.             next1,            /* point to next rcd # */
  311.             msg_delete,        /* delete byte */
  312.             bufmsg0);        /* piece of msg */
  313. #ifdef DEBUG
  314.         if (debug)
  315.         {
  316.             printf("after sprintf, msg_text = ");
  317.             prthex(msg_text);
  318.             printf("\n");
  319.             printf("after sprintf, buf128 = ");
  320.             prthex(buf128);
  321.             printf("\n");
  322.             printf("after sprintf, bufmsg0 = ");
  323.             prthex(bufmsg0);
  324.             printf("\n");
  325.         }
  326. #endif
  327.         cnt1 = tell(fd);
  328. #ifdef DEBUG
  329.         if (debug)
  330.         {
  331.             printf("tell() value = %d\n",cnt1);
  332.         }
  333. #endif
  334.         rc = write(fd,buf128,1);    /* write the n+1 128 byte record */
  335. #ifdef DEBUG
  336.         if (debug)
  337.         {
  338.             printf("after write of n+1 return code = %d\n",rc);
  339.             printf("after write of n+1 buf128 = ");
  340.             prthex(buf128);
  341.             printf("\n");
  342.         }
  343. #endif
  344.     }
  345.  
  346.     strfill(buf128,26,MSGSECT);        /* fill with all hex 1a */
  347.     cnt1 = tell(fd);
  348. #ifdef DEBUG
  349.     if (debug)
  350.     {
  351.         printf("tell() value = %d\n",cnt1);
  352.     }
  353. #endif
  354.     rc = write(fd,buf128,1);    /* write the all hex 1a 128 byte record */
  355. #ifdef DEBUG
  356.     if (debug)
  357.     {
  358.         printf("after write of all hex 1a return code = %d\n",rc);
  359.         printf("after write of hex 1a buf128 = ");
  360.         prthex(buf128);
  361.         printf("\n");
  362.         printf("leaving msgwrt\n");
  363.     }
  364. #endif
  365.     return(OK);
  366. }
  367.  
  368. msgrewrt(fd,r_msg)    /* re-write the message file from memory variables */
  369. int    fd,        /* re-writes only the 1st part of a message */
  370.     r_msg;        /* used to update the delete byte */
  371. {
  372.     int    rc,            /* return code */
  373.         cnt1,
  374.         file_size;
  375.     char    buf128[MSGSECT],
  376.         this1[10],
  377.         next1[10];
  378.  
  379.     rc = cnt1 = 0;
  380. #ifdef DEBUG
  381.     if (debug)
  382.     {
  383.         printf("in msgrewrt\n");
  384.         printf("rewrite msg = %d\n",r_msg);
  385.     }
  386. #endif
  387.     file_size = rcfsiz(fd);        /* find how many 128 byte sectors */
  388. #ifdef DEBUG
  389.     if (debug)            /*  are in the file and don't seek */
  390.     {                /*  past that */ 
  391.         printf("file_size = %d<%04x>\n",file_size,file_size);
  392.     }
  393. #endif
  394.     if (r_msg >= (file_size - 1))    /* don't try to seek past end of file */
  395.     {
  396.         return(ERROR);
  397.     }
  398.     if ((rc = seek(fd,r_msg,0)) == ERROR)    /* seek to requested sector */
  399.     {
  400.         return(ERROR);
  401.     }
  402. #ifdef DEBUG
  403.     if (debug)
  404.     {
  405.         printf("f